package com.hzcx.app.simplechat.ui.setting.adapter;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.setting.bean.AutoSendMessageBean;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.util.List;

public class SettingAutoSendMsgAdapter extends BaseQuickAdapter<AutoSendMessageBean, BaseViewHolder> {

    private static final int TYPE_TEXT = 1;
    private static final int TYPE_IMAGE = 2;

    private static final int TEXT = R.layout.rv_item_auto_send_msg_text;
    private static final int IMAGE = R.layout.rv_item_auto_send_msg_image;

    private boolean isLeft = true;

    public SettingAutoSendMsgAdapter(@Nullable List<AutoSendMessageBean> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<AutoSendMessageBean>() {
            @Override
            protected int getItemType(AutoSendMessageBean messageSettingBean) {
                if (messageSettingBean.getHost_type() == 2) {
                    return TYPE_IMAGE;
                } else {
                    return TYPE_TEXT;
                }
            }
        });

        getMultiTypeDelegate().registerItemType(TYPE_TEXT, TEXT)
                .registerItemType(TYPE_IMAGE, IMAGE);
    }

    @Override
    protected void convert(BaseViewHolder helper, AutoSendMessageBean item) {

        SwipeMenuLayout swipe_msg = helper.getView(R.id.swipe_msg);
        if (isLeft) {
            swipe_msg.setSwipeEnable(true);
            helper.setVisible(R.id.iv_move, false);
            helper.setVisible(R.id.tv_edit, true);
        } else {
            swipe_msg.setSwipeEnable(false);
            helper.setVisible(R.id.iv_move, true);
            helper.setVisible(R.id.tv_edit, false);
        }

        if (item.getIs_sel() == 1) {
            helper.setImageResource(R.id.iv_check_box, R.mipmap.ic_icon_check_select);
        } else {
            helper.setImageResource(R.id.iv_check_box, R.mipmap.ic_icon_check_normal);
        }

        helper.setText(R.id.tv_content, item.getMaincontent());

        if (item.getHost_type() == TYPE_IMAGE) {
            GlideUtils.loadImgAsCenter(mContext, item.getHostimage(), helper.getView(R.id.iv_img));
        }

        helper.addOnClickListener(R.id.iv_check_box, R.id.tv_delete, R.id.tv_edit);
    }

    public void setIsLeft(boolean isLeft) {
        this.isLeft = isLeft;
        notifyDataSetChanged();
    }
}
